home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / VGX / blob / vect.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  3.9 KB  |  236 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  * vect -
  19.  *    Various functions to support operations on vectors.
  20.  *
  21.  * David M. Ciemiewicz, Mark Grossman, Henry Moreton, and Paul Haeberli
  22.  *
  23.  * Modified for my own nefarious purposes-- Gavin Bell
  24.  *
  25.  * Modified for my own devine and holy purposes-- Wade Olsen
  26.  */
  27. #include "vect.h"
  28.  
  29. float    z_hat[3] = { 0, 0, 1 };
  30.  
  31.  
  32.  
  33. float *
  34. vnew()
  35. {
  36.     register float *v;
  37.  
  38.     v = (float *) malloc(sizeof(float)*3);
  39.     return v;
  40. }
  41.  
  42. float *
  43. vclone(v)
  44. float *v;
  45. {
  46.     register float *c;
  47.  
  48.     c = vnew();
  49.     vcopy(v, c);
  50.     return c;
  51. }
  52.  
  53. void
  54. vcopy(v1,v2)
  55. float *v1, *v2;
  56. {
  57.     register int i;
  58.     for (i = 0 ; i < 3 ; i++)
  59.         v2[i] = v1[i];
  60. }
  61.  
  62. void
  63. vprint(v)
  64. float *v;
  65. {
  66.     printf("x: %f y: %f z: %f\n",v[0],v[1],v[2]);
  67. }
  68.  
  69. void
  70. vset(float *v, float x, float y, float z)
  71. {
  72.     v[0] = x;
  73.     v[1] = y;
  74.     v[2] = z;
  75. }
  76.  
  77. void
  78. vzero(v)
  79. float *v;
  80. {
  81.     v[0] = 0.0;
  82.     v[1] = 0.0;
  83.     v[2] = 0.0;
  84. }
  85.  
  86. void
  87. vnormal(v)
  88. float *v;
  89. {
  90.     vscale(v,1.0/vlength(v));
  91. }
  92.  
  93. float
  94. vlength(v)
  95. float *v;
  96. {
  97.     return fsqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
  98. }
  99.  
  100. void
  101. vscale(float *v, float div)
  102. {
  103.     v[0] *= div;
  104.     v[1] *= div;
  105.     v[2] *= div;
  106. }
  107.  
  108. void
  109. vmult(src1,src2,dst)
  110. float *src1, *src2, *dst;
  111. {
  112.     dst[0] = src1[0] * src2[0];
  113.     dst[1] = src1[1] * src2[1];
  114.     dst[2] = src1[2] * src2[2];
  115. }
  116.  
  117. void
  118. vadd(src1,src2,dst)
  119. float *src1, *src2, *dst;
  120. {
  121.     dst[0] = src1[0] + src2[0];
  122.     dst[1] = src1[1] + src2[1];
  123.     dst[2] = src1[2] + src2[2];
  124. }
  125.  
  126. void
  127. vsub(src1,src2,dst)
  128. float *src1, *src2, *dst;
  129. {
  130.     dst[0] = src1[0] - src2[0];
  131.     dst[1] = src1[1] - src2[1];
  132.     dst[2] = src1[2] - src2[2];
  133. }
  134.  
  135. void
  136. vhalf(v1,v2,half)
  137. float *v1, *v2, *half;
  138. {
  139.     float len;
  140.  
  141.     vadd(v2,v1,half);
  142.     len = vlength(half);
  143.     if(len>0.0001)
  144.         vscale(half,1.0/len);
  145.     else
  146.         *half = *v1;
  147. }
  148.  
  149. float
  150. vdot(v1,v2)
  151. float *v1, *v2;
  152. {
  153.     return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
  154. }
  155.  
  156. void
  157. vcross(v1, v2, cross)
  158. float *v1, *v2, *cross;
  159. {
  160.     float temp[3];
  161.  
  162.     temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
  163.     temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
  164.     temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
  165.     vcopy(temp, cross);
  166. }
  167.  
  168. void
  169. vdirection(v1, dir)
  170. float *v1, *dir;
  171. {
  172.     *dir = *v1;
  173.     vnormal(dir);
  174. }
  175.  
  176. void
  177. vreflect(in,mirror,out)
  178. float *in, *mirror, *out;
  179. {
  180.     float temp[3];
  181.  
  182.     vcopy(mirror, temp);
  183.     vscale(temp,vdot(mirror,in));
  184.     vsub(temp,in,out);
  185.     vadd(temp,out,out);
  186. }
  187.  
  188. void
  189. vmultmatrix(m1,m2,prod)
  190. float m1[4][4], m2[4][4], prod[4][4];
  191. {
  192.     register int row, col;
  193.     float temp[4][4];
  194.  
  195.     for(row=0 ; row<4 ; row++) 
  196.         for(col=0 ; col<4 ; col++)
  197.             temp[row][col] = m1[row][0] * m2[0][col]
  198.                            + m1[row][1] * m2[1][col]
  199.                            + m1[row][2] * m2[2][col]
  200.                            + m1[row][3] * m2[3][col];
  201.     for(row=0 ; row<4 ; row++) 
  202.         for(col=0 ; col<4 ; col++)
  203.         prod[row][col] = temp[row][col];
  204. }
  205.  
  206. vtransform(v,mat,vt)
  207. float *v;
  208. float mat[4][4];
  209. float *vt;
  210. {
  211.     float t[3];
  212.  
  213.     t[0] = v[0]*mat[0][0] + v[1]*mat[1][0] + v[2]*mat[2][0] + mat[3][0];
  214.     t[1] = v[0]*mat[0][1] + v[1]*mat[1][1] + v[2]*mat[2][1] + mat[3][1];
  215.     t[2] = v[0]*mat[0][2] + v[1]*mat[1][2] + v[2]*mat[2][2] + mat[3][2];
  216.     vcopy(t, vt);
  217. }
  218.  
  219.  
  220. float vdistance(v1, v2)
  221. float *v1, *v2;
  222. {
  223.     return(sqrt( (v1[0] - v2[0]) * (v1[0] - v2[0])
  224.            + (v1[1] - v2[1]) * (v1[1] - v2[1])
  225.            + (v1[2] - v2[2]) * (v1[2] - v2[2])));
  226. }
  227.  
  228.  
  229. void vinterp(float *v1, float *v2, float t, float *vd)
  230. {
  231.     int        i;
  232.  
  233.     for (i = 0; i < 3; i++)
  234.     vd[i] = v1[i] + t * (v2[i] - v1[i]);
  235. }
  236.